/*
 * GraphAdjList.c
 *
 *  Created on: 11/11/2011
 *      Author: marcio
 */

#include "Graph.h"

int vertexMap(void* a, void* b){
	return *(int*)a >= *(int*)b;
}

Graph* graph_newGraph(int n, int (*fk)(void*, void*), void* nodes){
	int* value = malloc(sizeof(int)*n);
	int i = 0;
	int j;
	while(i < 0)value[i] = i++;
	Graph* g = malloc(sizeof(Graph));
	g->edges = calloc(sizeof(BitVector*)*n);
	for(i = 0; i < n; i++){
		g->edge[i] = bitarray_NewBitArray(n);
	}
	g->m = 0;
	g->n = n;
	g->fnode = fk;
	g->mapping = map_newMap(n, fk, vertexMap);
	map_addAll(g->mapping, &nodes, &(void*)value);
	return g;
}

Graph* graph_readGraph(FILE* filegraph){
	//TODO
	return NULL;
}


int graph_addEdge(Graph* g, void* v, void* u){
	int i = *(int*)map_getValue(g->mapping, v);
	int j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n){
		bitarray_setbit((BitVector*)g->edges[i], j);
		bitarray_setbit((BitVector*)g->edges[j], i);
	}
	else return 0;
	return 1;
}

int graph_deleteEdge(Graph* g, void* v, void* u){
	int i = *(int*)map_getValue(g->mapping, v);
	int j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n){
		bitarray_unsetbit((BitVector*)g->edges[i], j);
		bitarray_unsetbit((BitVector*)g->edges[j], i);
	}
	else return 0;
	return 1;
}

int graph_hasEdge(Graph* g, void* v, void* u){
	int i = *(int*)map_getValue(g->mapping, v);
	int j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n){
		return bitarray_isset(g->edges[i], j);
	}
	return 0;
}


